This page last changed on Mar 21, 2006 by mittie.

Quick Start

Create a Grails project

Once you have installed Grails you can use the built-in target for creating new projects:

grails create-app

The target will prompt you for the name of your project and create the project structure below:

%PROJECT_HOME%
    + grails-app
       + conf                 ---> location of configuration artifacts like data sources
       + controllers          ---> location of controller artifacts
       + domain               ---> location of domain classes
       + i18n                 ---> location of message bundles for i18n
       + services             ---> location of services
       + taglib               ---> location of tag libraries
       + views                ---> location of views
           + layouts              ---> location of layouts
   + lib
   + spring                    ---> optional spring config
   + hibernate                 ---> optional hibernate config
   + war
       + WEB-INF

Configure a Data Source

The "create-app" target created a Grails data source artifact for you in the "<..>/grails-app/conf" directory. By default an in-memory HSQLDB is configured (great for testing, but probably not that useful for live deployment):

ApplicationDataSource.groovy
class ApplicationDataSource {
   @Property String url = "jdbc:hsqldb:mem:testDB"
   @Property String driverClassName = "org.hsqldb.jdbcDriver"
   @Property String username = "sa"
   @Property String password = ""
}

Configuring the data source is a simple matter of changing the values for the desired database and driver and placing the driver jar file in the <..>/lib directory

Create a Domain Class

Make sure you are in the root directory of your project (for argument sake "my-project) by typing "cd my-project" then run the "grails create-domain-class" target and type in the name of your domain class. A domain class is a persistent artifact and all properties are by default persisted to the database (Go the the section on GORM (Grails Object Relational Mapping) for more info):

Book.groovy
class Book {
    @Property Long id
    @Property Long version

    @Property String title
    @Property String author
}

At this point you may want to create some test data, an appropriate place for this may be in the "init" closure of the Grails application bootstrap class found in "<..>/grails-app/conf":

new Book(author:"Stephen King",title:"The Shining").save()
new Book(author:"James Patterson",title:"Along Came a Spider").save()

Create the Controller

Controllers are central to Grails applications they handle web requests and URLs of the request map to a controller class and a closure within the class.

Run the "grails create-controller" target and type in the name of the controller. In our example we type "book" which creates the following class (see the Grails Controllers section for more information on working with controllers):

BookController.groovy
class BookController {
   @Property index = {
         redirect(action:list)
   }
   @Property list = {
             [ books : Book.list() ]
   }
}

Create the View

The view needs to be placed in a new directory called "book" in the "<..>/grails-app/views" directory and the file called "list.gsp" (i.e. named the same as the "list" closure within the "BookController" class).

The code below demonstrates a simple table listing, the "books" variable referenced by the "for" loop's item attribute matches the key found in the map returned by the controller. The example below uses Grails' inbuilt support for Groovy Server Pages (GSP) but standard JSP tags would also work.

grails-app/views/book/list.gsp
<html>
<head>
    <title>Book list</title>
</head>
<body>
<h1>Book list</h1>
<table>
    <tr>
        <th>Title</th>
         <th>Author</th>
    </tr>

    <% books.each { %> 
        <tr>
             <td>${it.title}</td>
             <td>${it.author}</td>
        </tr>
    <% } %>
</table>
</body>
</html>

Start Grails

To start your Grails app run the following target

grails run-app

This will startup in instance of the Jetty servlet engine running on port 8080. In order to start in on a different port like e.g. 9090 use grails -Dserver.port=9090 run-app. To access the list of books open up a browser and type:

http://localhost:8080/my-project/book/list

Or, as the "list" closure is the default action for the BookController you can type:

http://localhost:8080/my-project/book

 

Document generated by Confluence on Mar 29, 2006 08:46